home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CU Amiga Super CD-ROM 21
/
CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso
/
CUCD
/
Magazine
/
C_Tutorial
/
Part-9
/
wb2
/
main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-02-01
|
6KB
|
240 lines
#include<dos/rdargs.h>
#include<exec/libraries.h>
#include<workbench/startup.h>
#include<workbench/workbench.h>
#include<stdio.h>
#include<stdlib.h>
#include<clib/dos_protos.h>
#include<clib/exec_protos.h>
#include<clib/icon_protos.h>
#include "gui.h"
#include "idcmp.h"
#include "loadsave.h"
#include "arexx.h"
/* The library base global variables */
/* (The different style of opening libraries requires these to be initialised to NULL) */
struct Library* GfxBase = NULL;
struct Library* IntuitionBase = NULL;
struct Library* GadToolsBase = NULL;
struct Library* AslBase = NULL;
struct Library* DosBase = NULL;
struct Library* IFFBase = NULL;
struct Library* RexxSysBase = NULL;
struct Library* IconBase = NULL;
/* Need to give prototypes for our functions */
static int createAll(struct WBStartup*);
static void freeAll(void);
static int openLibs(void);
static void closeLibs(void);
/* The alternative "main()" for Workbench startup */
void wbmain(struct WBStartup*);
/* The shared starting point of our program */
static void realmain(struct WBStartup*);
#define TT_DEPTH "DEPTH"
#define TT_PORTNAME "PORTNAME"
#define ARGS_TEMPLATE "FILE," TT_DEPTH "/N," TT_PORTNAME
enum ARGS { ARG_FILENAME, ARG_DEPTH, ARG_PORTNAME, NUM_ARGS };
#define DEFAULT_PORTNAME "HELLOPAINTER"
#define DEFAULT_DEPTH (4)
/* The CLI starting point for StormC, but the general start for SAS/C */
void main(int argc, char** argv)
{
/* argc should never be zero: SAS/C uses this to indicate WB start */
if(argc == 0)
wbmain((struct WBStartup*)argv);
else
realmain(NULL);
}
/* The WB starting point for StormC */
void wbmain(struct WBStartup* wbmsg)
{
/* WB-specific startup could go here */
realmain(wbmsg);
}
/* The start of the program */
static void realmain(struct WBStartup* wbmsg)
{
if(createAll(wbmsg))
handleIDCMP();
freeAll();
}
static struct RDArgs* rdargs = NULL;
static struct DiskObject* dobj = NULL;
static int createAll(struct WBStartup* wbmsg)
{
int success = FALSE;
if(openLibs())
{
/* We'll only set portname if successful */
char* portname = NULL;
UBYTE depth;
char* filename = NULL;
BPTR currdir = NULL;
if(wbmsg)
{
/* WB bit, use Tool Types */
currdir = CurrentDir(wbmsg->sm_ArgList[0].wa_Lock);
if(dobj = GetDiskObject(wbmsg->sm_ArgList[0].wa_Name))
{
UBYTE** tt = (UBYTE**)dobj->do_ToolTypes;
char* depthptr = FindToolType(tt, TT_DEPTH);
portname = FindToolType(tt, TT_PORTNAME);
/* Use the default if a Tool Type was not specified */
if(portname == NULL)
portname = DEFAULT_PORTNAME;
if(depthptr)
depth = atoi(depthptr);
else
depth = DEFAULT_DEPTH;
}
else
printf("Error: could not read Tool Types\n");
/* Reset current directory, if we moved it */
if(currdir)
{
CurrentDir(currdir);
currdir = NULL;
}
if(wbmsg->sm_NumArgs > 1)
{
currdir = CurrentDir(wbmsg->sm_ArgList[1].wa_Lock);
filename = wbmsg->sm_ArgList[1].wa_Name;
}
}
else
{
/* CLI bit, use ReadArgs() */
LONG args[NUM_ARGS];
int i;
/* Initialise our args to NULL */
/* (This way we will know if an argument was specified) */
for(i=0; i<NUM_ARGS; i++)
args[i] = NULL;
if(rdargs = ReadArgs(ARGS_TEMPLATE, args, NULL))
{
LONG* depthptr = (LONG*)(args[ARG_DEPTH]);
portname = (char*)(args[ARG_PORTNAME]);
filename = (char*)(args[ARG_FILENAME]);
/* Use the default if an argument was not specified */
if(portname == NULL)
portname = DEFAULT_PORTNAME;
if(depthptr == NULL)
depth = DEFAULT_DEPTH;
else
depth = (UBYTE)(*depthptr);
}
else
printf("Error: could not read arguments\n");
}
if(portname)
{
if(createARexxPort(portname) && createArgs() && openGUI(depth,0,0,0))
{
success = TRUE;
if(filename)
loadfile(filename);
}
}
/* Reset current directory, if we moved it */
if(currdir)
CurrentDir(currdir);
}
return success;
}
static void freeAll()
{
freeReqs();
closeGUI();
freeArgs();
freeARexxPort();
if(rdargs)
FreeArgs(rdargs);
if(dobj)
FreeDiskObject(dobj);
closeLibs();
}
/* Try to open all the libraries -- return TRUE on success */
static int openLibs()
{
if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
{
printf("Error: could not open graphics.library\n");
return FALSE;
}
if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
{
printf("Error: could not open intuition.library\n");
return FALSE;
}
if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
{
printf("Error: could not open gadtools.library\n");
return FALSE;
}
if((AslBase = OpenLibrary("asl.library",37)) == NULL)
{
printf("Error: could not open asl.library\n");
return FALSE;
}
if((DosBase = OpenLibrary("dos.library",37)) == NULL)
{
printf("Error: could not open dos.library\n");
return FALSE;
}
if((IFFBase = OpenLibrary("iff.library",23)) == NULL)
{
printf("Error: could not open iff.library\n");
return FALSE;
}
if((RexxSysBase = OpenLibrary("rexxsyslib.library",35)) == NULL)
{
printf("Error: could not open rexxsyslib.library\n");
return FALSE;
}
if((IconBase = OpenLibrary("icon.library",37)) == NULL)
{
printf("Error: could not open icon.library\n");
return FALSE;
}
return TRUE;
}
/* Close any open library */
static void closeLibs()
{
if(IconBase)
CloseLibrary(IconBase);
if(RexxSysBase)
CloseLibrary(RexxSysBase);
if(IFFBase)
CloseLibrary(IFFBase);
if(DosBase)
CloseLibrary(DosBase);
if(AslBase)
CloseLibrary(AslBase);
if(GadToolsBase)
CloseLibrary(GadToolsBase);
if(IntuitionBase)
CloseLibrary(IntuitionBase);
if(GfxBase)
CloseLibrary(GfxBase);
}